<?php include "../public/header.php";?>
<?php include "../public/header_edit.php";?>
<link rel="stylesheet" type="text/css" href="/static/component/laytp/module/cityPicker/city-picker.css">

<body id="optPage">
<form class="layui-form layui-form-pane" id="commentForm">
    <div class="page-title"><div class="page-title-con"><span>设置计税规则</span><div class="move_title"></div><?php include "../public/sub.php";?></div></div>
    <div class="main-container">


        <div class="block-generic">
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label">项目名称</label>
                        <div class="layui-input-block">
                            <input type="text" id="name" readonly placeholder="请输入" clearable autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label">应交成本票比例(%)</label>
                        <div class="layui-input-block">
                            <input type="text" id="piaorate" readonly placeholder="请输入" clearable autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label">成本票差额费比例(%)</label>
                        <div class="layui-input-block">
                            <input type="text" id="charate" readonly placeholder="请输入" clearable autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="block-generic">
            <div class="layui-form-item height_auto">
                <div class="short_table_list" key="1">
                    <div class="opt-btn-group">
                        <div><span>计税规则</span></div>
                    </div>
                    <table class="layui-table" lay-size="sm">
                        <thead>
                        <tr>
                            <th width="45">序号</th>
                            <th>税种</th>
                            <th><text>*</text>计算项</th>
                            <th><text>*</text>计算值</th>
                            <th>计算说明</th>
                        </tr>
                        </thead>
                        <tbody class="short_table_tbody" id="list_list" style="max-height:400px"></tbody>
                    </table>
                    <div class="listerror">提示：<span></span></div>
                </div>
            </div>
        </div>

    </div>
</form>
<script>
    var param_id = getQueryVariable('id') || 0;


    var pub_xiang;
    layui.use(['laytp', 'form'], function() {
        var form = layui.form, layer = layui.layer;
        render_input_attr();

        facade.ajax({type: 'get', url: ask_engin_she,data:{id:param_id}}).done(function(res){
            if (res.code === 2000) {
                var data = res.data.data;

                $("#name").val(data.name);
                $("#piaorate").val(data.piaorate);
                $("#charate").val(data.charate);

                short_table_opt_func('', 1);

                var nameArr = res.data.name;
                var xiang = res.data.xiang;
                pub_xiang = res.data.xiang;

                var list = res.data.list;
                if(list.length > 0){
                    for (var key in list){
                        var str = '';
                        var nums = parseInt(key)+1;
                        str += '<tr>';
                        str += '<td style="width: 45px">'+nums+'</td>';
                        str += '<td><input type="text" class="layui-input name disabled" disabled key="'+list[key].name+'" value="'+nameArr[list[key].name]+'"></td>';

                        str += '<td><select class="xiang" lay-filter="xiang">';
                        str += '<option value="" selected>请选择</option>';
                        for (var mm in xiang){
                            if(mm == list[key].xiang){
                                str += '<option value="'+mm+'" selected>'+xiang[mm]+'</option>';
                            }else{
                                str += '<option value="'+mm+'">'+xiang[mm]+'</option>';
                            }
                        }
                        str += '</select></td>';

                        str += '<td><input type="number" class="layui-input rateval" value="'+list[key].rateval+'"></td>';
                        str += '<td><input type="text" class="layui-input explain disabled" value="'+set_explain(list[key].xiang,list[key].rateval)+'" disabled></td>';
                        str += '</tr>';
                        $("#list_list").append(str);
                    }
                }
                update_refresh_row_func()



                initParseMoney();
                render_from();
            }
        });

        form.on('select(xiang)', function(data){
            var rateval = $(this).parents('tr').find('.rateval').val();
            $(this).parents('tr').find('.explain').val(set_explain(data.value, rateval));
        });


        form.on('submit(add)', function(data){
            var validRes = $("#commentForm").valid();
            if(!validRes){return false;}


            var list = [''];
            try {
                $('#list_list tr').each(function (i) {
                    var name = $(this).find('.name').attr('key') || '';
                    var xiang = $(this).find('.xiang').val();
                    var rateval = $(this).find('.rateval').val();

                    var index = i + 1;
                    if(!xiang){
                        throw new Error('第'+index+'行，请选择计算项');
                    }
                    if(rateval == ''){
                        throw new Error('第'+index+'行，请输入计算值');
                    }

                    list.push({
                        name:name,
                        xiang:xiang,
                        rateval:rateval,
                    });
                });
            } catch(e) {
                $(".listerror").find('span').text(e.message);
                $(".listerror").show();
                return false;
            }
            $(".listerror").hide();
            data.field.list = list;



            data = parseMoney(data);
            data.field.id = param_id;
            facade.ajax({url: ask_engin_she, data:data.field}).done(function(res){
                if (res.code === 2000) {
                    dataPostTimeOut();
                }
            });

            return false;
        });

    });


    $('#list_list').on('input propertychange','.rateval',function () {
        var xiang = $(this).parents('tr').find('.xiang').val();
        var rateval = $(this).parents('tr').find('.rateval').val();
        $(this).parents('tr').find('.explain').val(set_explain(xiang, rateval));
    });




    function update_refresh_row_func() {
        render_date();
        render_from();
        $.each($("#list_list").children('tr'),function (key,val) {
            $(this).children('td:first-child').html(key+1);
        });
        setTimeout(function () {
            $(".short_table_list table tbody tr").click(function () {
                $(".short_table_list table tbody tr").removeClass('line-row');
                $(".short_table_list table tbody tr").attr('sle','');
                $(this).addClass('line-row');
                $(this).attr('sle','sle');
            });
        },1000);
    }




    function set_explain(xiang=0, rateval='') {
        var xiangName = pub_xiang[xiang] || '';
        if(!xiangName || !rateval || rateval == 0){
            return 0;
        }
        return xiangName+" X "+rateval;
    }

</script>
</body>
</html>
